package com.yixun.kafka;

import com.alibaba.fastjson.JSONObject;
import com.yixun.constant.SystemConstant;
import com.yixun.service.ThanNullService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @Author: yx
 * @Desc: kafka消费原数据，判断是否超过12个通道没有数据
 * @Date: 11:05 2021/5/8
 **/
@Component
@Slf4j
public class ThanNullConsumer {

    @Autowired
    private ThanNullService thanNullService;

    //消费原数据，不同消费组可以重复消费   只要用到一条数据就用kafka
    @KafkaListener(topics = {SystemConstant.SITE_DATA_TOPIC},groupId = SystemConstant.THAN_NULL_CONSUMER)
    public void onMessage(List<ConsumerRecord<?, ?>> list) {
        for (ConsumerRecord consumerRecord : list) {
            Optional<?> kafkaMessage = Optional.ofNullable(consumerRecord.value());
            if (kafkaMessage.isPresent()) {
                String json = String.valueOf(kafkaMessage.get());
                thanNullService.thanNull(json);
                System.out.println("kafka消费原数据，判断是否超过12个通道没有数据"+json);
            }
        }
    }
}
